import CreepNumberSettings from "@/app/creep/numberSettings";
import { CreepData, CreepRole } from "@/role/types/role";
import whiteList from "@/app/global/whiteList";
import res from "@/contrib/console/common/res"
import task from "@/contrib/console/common/task"


enum CmdReturnCode {
    OK, FAILED
}


export const calen = function (roomName: string = 'E35S49'): CmdReturnCode {

    const room = Game.rooms[roomName]
    if (room) {
        const extensions = room[STRUCTURE_EXTENSION]
        console.log('max energy', extensions.length * 50 + 300)
    }
    return CmdReturnCode.OK
}

export const calcost = function (price: number = 1.8, orderId: string = '', room: string = 'E35S49'): CmdReturnCode {

    // console.log(Game.cpu.getUsed())
    // const orders = Game.market.getAllOrders({
    //     resourceType: RESOURCE_ENERGY,
    //     type: ORDER_SELL
    // })
    const orders = Game.market.getAllOrders(
        order => order.resourceType == RESOURCE_ENERGY &&
            // order.type == ORDER_SELL &&
            // order.price <= price &&
            order.type == ORDER_BUY &&
            order.price > price &&
            Game.market.calcTransactionCost(10000, room, order.roomName) <= 5000
    )
    console.log(orders.length)

    Game.market.calcTransactionCost(10000, 'E29S21', 'E35S49')
    // const order = Game.market.getOrderById(orderId)
    // const roomName = order.roomName
    // const amount = order.amount
    // const cost = Game.market.calcTransactionCost(amount, roomName, room);
    // console.log(amount, cost, (cost / (cost + amount) * 100).toFixed(2) + '%')

    return CmdReturnCode.OK
}

export const deal = function (orderId: string, amount: number, room: string = 'E35S49'): CmdReturnCode {
    const status = Game.market.deal(orderId, amount, "E35S49");
    console.log(status)
    return CmdReturnCode.OK
}

export const createorder = function (price: number, amount: number): CmdReturnCode {

    Game.market.createOrder({
        type: ORDER_BUY,
        resourceType: RESOURCE_ENERGY,
        price: price,
        totalAmount: amount,
        roomName: "E35S49"
    });

    return CmdReturnCode.OK
}

export const cancelspawn = function (id: string): CmdReturnCode {

    const spawn = Game.getObjectById(id) as StructureSpawn
    const status = spawn.spawning.cancel()
    console.log(status)
    return CmdReturnCode.OK
}

export const attacktest = function (): CmdReturnCode {

    const spawn = (Game.rooms['E35S49'][STRUCTURE_SPAWN] as StructureSpawn[])[0]
    const data: CreepData = {
        workRoom: 'E35S49',
    }
    const status = CreepNumberSettings.doSpawnCreep(spawn, CreepRole.Attacker, data)
    console.log(spawn.room.name + ' [doSpawnCreep] Attacker' + status)
    return CmdReturnCode.OK
}


export default function () {
    _.assign(global, {
        res: res,
        task: task,
        whiteList: whiteList,
        calen: calen,
        calcost: calcost,
        deal: deal,
        createorder: createorder,
        cancelspawn: cancelspawn,
        attacktest: attacktest,
    })
}
